//
//  ABPableViewController.m
//  News
//
//  Created by xalo on 16/3/3.
//  Copyright © 2016年 News. All rights reserved.
//

#import "ABPableViewController.h"
#import "HTTPHelper.h"
#import "PicListModel.h"
#import <MJRefresh.h>

#import "TableViewCell.h"
#import "TwoTableViewCell.h"
#import "ThreeTableViewCell.h"
#import "FourTableViewCell.h"
#import "FiveTableViewCell.h"

#import "NewsDetailViewController.h"
#import "PicDetialViewController.h"
#import "ScrollPageView.h"
#import "FMDatabase+Database.h"
#import <Reachability.h>
#import <MBProgressHUD.h>

@interface ABPableViewController ()

@property (nonatomic, strong) NSMutableArray *selectArray;

@property (nonatomic ,strong) NSArray *array;

@property (nonatomic, strong) Reachability *reachabity;
@end

@implementation ABPableViewController


- (void)viewDidLoad {
    [super viewDidLoad];
   
    
    [self setNibIdentifitAction];
    
    
    //设置单元格的线条隐藏
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //下拉加载
    [self getDownData];
    
    //上拉刷新
    [self getUpData];
    
    //获取数据
    [self getData];
    
    
}



#pragma mark - Table view data source
//单元格个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.selectArray.count;
}

//设置单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    PicListModel *picModel = self.selectArray[indexPath.row];
    
        if (picModel.picTemplate == 1){
        
        TwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        
        cell.picMod = picModel;
        return cell;
    }
    else if (picModel.picTemplate == 2){
        
        ThreeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
        
        cell.picMod = picModel;
        
        return cell;
        
    }else if (picModel.picTemplate == 3){
        
        FiveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell4" forIndexPath:indexPath];
        
        cell.picMod = picModel;
        return cell;
    }
    else if ( picModel.picTemplate == 4) {
        
        FourTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell3" forIndexPath:indexPath];
        
        cell.picMod = picModel;
        return cell;
    }

    TableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    cell.picMod = picModel;
    
    return cell;
    
}


//设置单元格高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    PicListModel *picModel = self.selectArray[indexPath.row];

    if (picModel.picTemplate == 1) {
        
        return  picModel.cellHeight;
     
   }
    else if ( picModel.picTemplate == 2) {
        
    return picModel.cellHeight;
       
   }
   
    else if (picModel.picTemplate == 3) {
       
        return picModel.cellHeight;
   }
   
   else if ( picModel.picTemplate == 4) {
       
   return picModel.cellHeight;
   }

    return  picModel.cellHeight;
   
}

//guji gaod u
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return self.view.frame.size.width *0.3;
    
}

//点击单元格,进入详情
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self s ]) {
        
        PicDetialViewController *picdetial = [[UIStoryboard storyboardWithName:@"Picture" bundle:nil]instantiateViewControllerWithIdentifier:@"PicDetial"];

        PicListModel *picModel = self.selectArray[indexPath.row];
        
        picdetial.comments = picModel.ID;
        picdetial.link  = picModel.title;
        picdetial.picLink =picModel.link;
        
               
        [self presentViewController:picdetial animated:YES completion:nil];
        
    }
    else {
        
        
        [self monitorAction];
    }
    
}

//设置注册
-(void)setNibIdentifitAction {
    
    [self.tableView registerNib:[UINib nibWithNibName:@"TableViewCell" bundle:nil ] forCellReuseIdentifier:@"cell" ];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"TwoTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell1"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ThreeTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell2"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"FourTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell3"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"FiveTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell4"];
    
}


//上拉加载
-(void)getUpData {
    
        self.tableView.mj_header =[MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            //判断网络状态
            if ([self s]) {
                
                [[HTTPHelper sharedManger] getDropDownLoadDataWith:self.channel number:self.times picArray:self.selectArray block:^{
                              
                [self.tableView reloadData];
                    
                [self.tableView.mj_header endRefreshing];
            }
                 // 前面的block 请求数据成功后,执行这个block里面的方法,进行一次回调
            reblock:^{
                
                [self.selectArray  removeAllObjects];
                
                [self.tableView reloadData];
                
                self.times = 1;
                
            }];
         
              }else {
               //没有网络,加载缓存数据
                [self reachCache];
                [self monitorAction];
                
                [self.tableView.mj_header endRefreshing];
            }
               
        }];
    

    
}

//下拉刷新数据
-(void)getDownData {
    
    //下拉刷新
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        self.times++;
        
        if([self s]){
        
        [[HTTPHelper sharedManger]  getDropUpLoadDataWith:self.channel number:self.times picArray:self.selectArray block:^{
            
            [self.tableView.mj_footer endRefreshing];
            
            [self.tableView reloadData];
            
            
        }];
        
        }else {
            
            [self monitorAction];
        
           }
        
    }];
}

//读取缓存
-(void)reachCache{
    
    NSArray *array = [[FMDatabase shareManager] selectNewsModelArrayWithChannel:self.channel times:self.times];
    
    if (array  == nil) {
        
        return ;
    }else {
        
        for (NSDictionary *dic in array) {
            
            PicListModel *list = [[PicListModel alloc]init];
            
            [list setValuesForKeysWithDictionary:dic];
            
            [self.selectArray addObject:list];
        }
    }
        
    [self.tableView reloadData];
}

//获取数据
-(void)getData {

    //菊花转
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    //判断网络
    if ([self s]) {
    
        [[HTTPHelper sharedManger] fetchPictureDataWithUrl:self.channel picArray:self.selectArray block:^{
            
            [self.tableView reloadData];
            
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
 
        }];
        
    }
    else{
    
        //读取缓存
        [self  reachCache];
        
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        
        
    }
    
}

//判断网络状态
-(BOOL)s {
    
    BOOL state;
    
    Reachability *reach = [Reachability reachabilityForInternetConnection];
    
    if ([reach currentReachabilityStatus]) {
        
        return state = YES;
        
    }
    
     return state = NO;
}

//提示框
-(void)monitorAction {
    
    UIAlertController *a =[UIAlertController alertControllerWithTitle:@"提示" message:@"没有网络,小君也没办法..." preferredStyle:(UIAlertControllerStyleAlert)
                           ];
   
    [self presentViewController:a animated:YES completion:nil];
    
    
    [self performSelector:@selector(diss) withObject:self afterDelay:1.0];
    
    
}

//提示框消失时间
-(void)diss {
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

//懒加载
-(NSMutableArray *)selectArray {
    
    
    if (!_selectArray) {
        
        _selectArray = [NSMutableArray array];
    }
    
    return _selectArray;
    
}

//通知,一键换肤
-(void)changBackGroundView {
    
        self.tableView.backgroundView = ({
        UIView *view= [[UIView alloc] init];
        view.backgroundColor = [UIColor blueColor];
        view;
    });
 
}



@end
